import pygame
import os
import random

from pygame.locals import *

class BrickProperties(object):
    def __init__(self):
        #each brick has a random colour
        
        min_value = 72 #we don't want bricks to be too dark
        variation = 255 - min_value
        r = min_value + int(random.random() * variation)
        g = min_value + int(random.random() * variation)
        b = min_value + int(random.random() * variation)
        
        self.color = (r, g, b)

class Bricks(object):  
    def __init__(self):
        #load texture
        texture_path = os.path.join("img", "brick.png")
        self.texture = pygame.image.load(texture_path)
        
        #amount of the screen to fill with bricks
        field_amount_h = 0.9
        field_amount_v = 0.6
        field_width = pygame.display.get_surface().get_width() * field_amount_h
        field_height = pygame.display.get_surface().get_height() * field_amount_v
        self.brick_count_h = int(field_width) / self.texture.get_width()
        self.brick_count_v = int(field_height) / self.texture.get_height()
        
        total_block_width = self.brick_count_h * self.get_brick_width()
        
        self.x = pygame.display.get_surface().get_width() / 2 - total_block_width / 2
        self.y = self.get_brick_height()
        
        #bricks are stored in a list, each list entry is either a BrickProperties object or None if the brick is destroyed
        self.bricks = list()
        for brick_x in range(self.brick_count_h):
            self.bricks.append(list())
            
            for brick_y in range(self.brick_count_v):
                self.bricks[brick_x].append(BrickProperties())
                
    def get_brick_width(self):
        return self.texture.get_width()
        
    def get_brick_height(self):
        return self.texture.get_height()
        
    def brick_hit_test(self, pos):
        #convert screen coords to coords in the array
        x = (int(pos[0]) - self.x) / self.get_brick_width()
        y = (int(pos[1]) - self.y) / self.get_brick_height()
        
        if x >= 0 and x < self.brick_count_h and y >= 0 and y < self.brick_count_v and self.bricks[x][y] != None:
            result_x = self.x + (x * self.get_brick_width())
            result_y = self.y + (y * self.get_brick_height())
            
            result_rect = pygame.Rect(result_x, result_y, self.get_brick_width(), self.get_brick_height())
            result_color = self.bricks[x][y].color
            
            #break the brick
            self.bricks[x][y] = None
            
            return (result_rect, result_color)
        else:
            return False
        
    def draw(self, screen):
        brick_rect = pygame.Rect(0,0,0,0)
        brick_rect.width = self.get_brick_width()
        brick_rect.height = self.get_brick_height()
    
        for x in range(self.brick_count_h):
            for y in range(self.brick_count_v):
                brick = self.bricks[x][y]
                if brick != None:
                    brick_rect.left = self.x + (x * brick_rect.width)
                    brick_rect.top = self.y + (y * brick_rect.height)
                    
                    screen.blit(self.texture, (brick_rect.left, brick_rect.top))
                    screen.fill(brick.color, brick_rect, BLEND_RGBA_MULT)                